//
//  CTVTextEntryController.m
//  Channltv
//
//  Created by Bruno  Keymolen on 18/07/07.
// A wrapper arround the BRTextEntryControl
//  Copyright 2007 channl.tv. All rights reserved.
//

#import "CTVTextEntryController.h"


@implementation CTVTextEntryController

- (id) initWithScene: (BRRenderScene *) scene andTitle:title andValue:(NSString*)value andLabel:(NSString*)label
{
    if ( [super initWithScene: scene] == nil )
        return ( nil );

    // Setup the Header Control with default contents
    _header = [[BRHeaderControl alloc] initWithScene: scene];
    [_header setTitle: title];

    NSRect frame = [[self masterLayer] frame];
    frame.origin.y = frame.size.height * 0.82f;
    frame.size.height = [[BRThemeInfo sharedTheme] listIconHeight];
    [_header setFrame: frame];

 
    // setup the text entry control
    _entry = [[BRTextEntryControl alloc] initWithScene: scene];
    [_entry setFrameFromScreenSize: [[self masterLayer] frame].size];
    [_entry setTextFieldLabel: label];
    [_entry setInitialText: value];
    [_entry setTextEntryCompleteDelegate: self];

    // add controls
    [self addControl: _header];
    [self addControl: _entry];

    return ( self );
}

- (void) dealloc
{
    [_entry setTextEntryCompleteDelegate: nil];
    [_header release];
    [_entry release];
	[_textvalue release];
    [super dealloc];
}

- (void) textDidChange: (id<BRTextContainer>) sender
{
    // do nothing for now
}

- (void) textDidEndEditing: (id<BRTextContainer>) sender
{
	NSString* textval = [sender stringValue];
	[textval retain]; //ensure that the text will stay in memory until we are done
	[_textvalue release];
	_textvalue = textval;
	
	//Tell the listeners we are done
	NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
	[nc postNotificationName:@"CTVTextEntryControllerDone" object:self];
}

-(void) addObserver:(id)observer withSelector:(SEL)CTVselector{
	NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
	[nc addObserver:observer selector:CTVselector name:@"CTVTextEntryControllerDone" object:nil];
}


- (void) removeControl: (BRControl *) control
{
    NSMutableArray * array = [NSMutableArray arrayWithArray: [self controls]];
    if ( [array containsObject: control] )
    {
        [[control layer] removeFromSuperlayer];
        [array removeObject: control];
        [self setControls: array];
    }
}

- (void) fadeFrom: (BRControl *) from to: (BRControl *) to
{
    [to setAlphaValue: 0.0f];
    [self addControl: to];

    BRValueAnimation * valanim = [BRValueAnimation fadeInAnimationWithTarget: to
                                  scene: [self scene]];

    BRAggregateAnimation * animation = [BRAggregateAnimation animationWithScene: [self scene]];
    [animation setDuration: [[BRThemeInfo sharedTheme] fadeThroughBlackDuration]];
    [animation addAnimation: valanim];

    valanim = [BRValueAnimation fadeOutAnimationWithTarget: from
                                                     scene: [self scene]];
    [animation addAnimation: valanim];
    [animation run];

    // remove the leaving control so it doesn't eat events
    [self removeControl: from];
}



-(NSString*)textvalue{
	return _textvalue;
}


@end
